package com.freesky.provider.order.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.dao.entity.Express;
import com.freesky.provider.order.service.IExpressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Freesky
 */
@Service
public class ExpressServiceImpl implements IExpressService {
    Logger logger = LoggerFactory.getLogger(ExpressServiceImpl.class);
    @Override
    public List<Express> getExpressList() {
        return new Express().selectList(new EntityWrapper().orderBy("sort_order", true));
    }

    @Override
    public JSONResponse addExpress(Express express) {
        express.setCreated(new Date());
        express.insert();
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse updateExpress(Express express) {
        express.setUpdated(new Date());
        express.updateById();
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse delExpress(int[] ids) {
        for (int id : ids) {
            Express express = new Express(id);
            try {
                express.deleteById();
            } catch (Exception e) {
                logger.error("删除快递失败",e);
                return JSONResponseDiretor.buildErrorJSONResponse(null);
            }
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }
}
